// pages/fenlei/fenlei.js
import request from "../../utils/request"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsCount: 0,
    categoryList: [],
    id: 0,
    currentCate: null,
    page: 1,
    size: 6,
    totalPages: 0,
    goodsList: [],
    hasMore: true
  },
  dian1(e) {
    this.setData({
      id: 0
    })
  },
  dian2(e) {
    this.setData({
      id: e.currentTarget.dataset.id
    }, function () {
      this.getCurrentCate();
      this.CateGoods();
    })
  },
  handleLoadMore(){
    this.setData({
      page: this.data.page + 1
    }, function(){
      this.CateGoods();
    })
  },
  async getGoodsCount() {
    let data = await request({
      url: '/goods/count',
      method: 'GET'
    });
    this.setData({
      goodsCount: data.goodsCount
    })
  },
  async getCurrentPages() {
    let data = await request({
      url: '/catalog/index',
      method: 'GET'
    });
    this.setData({
      categoryList: data.categoryList
    })
  },
  async getCurrentCate() {
    let id = this.data.id;
    let data = await request({
      url: '/catalog/current?id=' + id,
      method: "GET"
    })
    this.setData({
      currentCate: data,
      page: 1,
      goodsList: []
    })
  },
  async CateGoods(id) {
    let data = await request({
      url: '/catalog/currentlist',
      method: 'POST',
      data: {
        size: this.data.size,
        page: this.data.page,
        id: this.data.id
      }
    })

    this.setData({
      goodsList: [...this.data.goodsList, ...data.data],
      totalPages: data.totalPages,
      hasMore: this.data.page<data.totalPages
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getGoodsCount();
    this.getCurrentPages();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})